from fastapi import APIRouter, Request
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, get_redoc_html
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from apps.core.m_route import ContextIncludedRoute

router = APIRouter(route_class=ContextIncludedRoute)
templates = Jinja2Templates(directory="apps/docs/templates")


@router.get("/static", response_class=HTMLResponse)
async def read_item(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


@router.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    from app_main import app
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title=app.title + " - Swagger UI",
        oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
        swagger_js_url="/mdoc/static/swagger-ui-bundle.js",
        swagger_css_url="/mdoc/static/swagger-ui.css",
    )


# app.swagger_ui_oauth2_redirect_url = "/docs/oauth2-redirect"
@router.get("/docs/oauth2-redirect", include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()


@router.get("/redoc", include_in_schema=False)
async def redoc_html():
    from app_main import app
    return get_redoc_html(
        openapi_url=app.openapi_url,
        title=app.title + " - ReDoc",
        redoc_js_url="/mdoc/static/redoc.standalone.js",
    )
